import 'package:faker/i18n/i18n.dart';
import 'package:faker/models/article.dart';
import 'package:faker/utils/prase_util.dart';
import 'package:faker/utils/route_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:intl/intl.dart' as intl;
import 'package:webfeed/webfeed.dart';

class RssDetailsList extends StatefulWidget {
  /// TODO实体
  final RssItem item;

  final bool fav;

  final bool mark;

  /// 滑动删除Controller
  SlidableController slidableController;

  /// 更新TODO
  Function addToCollection;

  /// 删除TODO
  Function readLater;

  RssDetailsList({
    this.item,
    this.fav,
    this.mark,
    this.slidableController,
    this.addToCollection,
    this.readLater,
  });

  @override
  State<StatefulWidget> createState() {
    return new RssDetailsListState();
  }
}

class RssDetailsListState extends State<RssDetailsList> {
  bool f = false;
  bool r = false;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    f = widget.fav;
    r = widget.mark;
  }

  @override
  Widget build(BuildContext context) {
    var item = widget.item;
    SlidableController slidableController = widget.slidableController;
    return Material(
      color: Theme.of(context).backgroundColor,
      shape: new RoundedRectangleBorder(
        side: const BorderSide(
          width: 0.382,
          style: BorderStyle.solid,
        ),
        //每个角落的半径
        borderRadius: new BorderRadius.circular(6.0),
      ),
      child: Slidable(
        controller: slidableController,
        actionPane: SlidableDrawerActionPane(),
        actionExtentRatio: 0.25,
        child: InkWell(
          onTap: () {
            RouteUtil.toWebView(context, item.title, item.link);
            //RouteUtil.push(context,ArticleView(title: widget.item.title,data: widget.item.description,));
          },
          child: Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.fromLTRB(16, 8, 16, 8),
                      child: Column(
                        children: <Widget>[
                          Container(
                            alignment: Alignment.topLeft,
                            padding: EdgeInsets.fromLTRB(0, 2, 0, 2),
                            child: Text(
                              item.title,
                              style: TextStyle(
                                  fontSize: 16,
                                  color: Theme.of(context).textSelectionColor),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              textAlign: TextAlign.left,
                            ),
                          ),
                          Container(
                            alignment: Alignment.topLeft,
                            padding: EdgeInsets.fromLTRB(0, 8, 0, 2),
                            child: Text(
                              PraseUtil.removeTag(htmlString: item.description),
                              style: TextStyle(
                                  fontSize: 15, color: Color(0xFF757575)),
                              maxLines: 3,
                              overflow: TextOverflow.ellipsis,
                              textAlign: TextAlign.left,
                            ),
                          ),
                          Container(
                            alignment: Alignment.topLeft,
                            padding: EdgeInsets.fromLTRB(0, 8, 0, 2),
                            child: Text(
                              item.pubDate == null ? "木有日期" : item.pubDate,
                              style: TextStyle(
                                  fontSize: 15, color: Color(0xFF757575)),
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                ],
              ),
              Divider(height: 1),
            ],
          ),
        ),
        secondaryActions: <Widget>[
          IconSlideAction(
            caption: f ? S.of(context).undofavourite : S.of(context).favourite,
            color: Theme.of(context).backgroundColor,
            icon: f ? Icons.favorite : Icons.favorite_border,
            onTap: () {
              ///收藏;
              Article article = new Article();
              article.title = item.title == null ? "" : item.title;
              article.guid = item.guid == null ? "" : item.guid;
              article.author = item.author == null ? "" : item.author;
              article.description =
                  item.description == null ? "" : item.description;
              article.link = item.link == null ? "" : item.link;
              article.pubDate = item.pubDate == null
                  ? ""
                  : intl.DateFormat('yyyy-MM-dd')
                      .format(DateTime.now())
                      .toString();
              widget.addToCollection(article, f);
              setState(() {
                f = f ? false : true;
              });
            },
          ),
          IconSlideAction(
            caption: r ? S.of(context).unmark : S.of(context).mark,
            color: Theme.of(context).backgroundColor,
            icon: r ? Icons.watch_later : Icons.cancel,
            onTap: () {
              ///稍后阅读;
              Article article = new Article();
              article.title = item.title == null ? "" : item.title;
              article.guid = item.guid == null ? "" : item.guid;
              article.author = item.author == null ? "" : item.author;
              article.description =
                  item.description == null ? "" : item.description;
              article.link = item.link == null ? "" : item.link;
              article.pubDate = item.pubDate == null
                  ? ""
                  : intl.DateFormat('yyyy-MM-dd')
                      .format(DateTime.now())
                      .toString();
              widget.readLater(article, r);
              setState(() {
                r = r ? false : true;
              });
            },
          ),
        ],
      ),
    );
  }
}
